package server

import (
	"log"
	"myygo/internal/util/net"
	"sync"
)

type Room struct {
	ID        string
	Players   map[string]*Player
	Broadcast chan *pb.GameMessage
	mu        sync.RWMutex
}

type Player struct {
	Session *net.Session
	Deck    *pb.Deck
	IsReady bool
}

func NewRoom(player1, player2 *net.Session) *Room {
	return &Room{
		ID: generateRoomID(),
		Players: map[string]*Player{
			player1.UserID: {Session: player1},
			player2.UserID: {Session: player2},
		},
	}
}

func (r *Room) Start() {
	go r.run() // 维护准备的过程

	// 两个用户都准备完毕后开启游戏
}

func (r *Room) run() {

}

// 对玩家进行广播
func (r *Room) broadcast(data []byte) {
	r.mu.RLock()
	defer r.mu.RUnlock()

	for _, player := range r.Players {
		select {
		case player.Session.SendChan <- data:
		default:
			log.Println("玩家消息队列已满:", player.Session.UserID)
		}
	}
}
